<?php

/*
*/

class Sale_Advice_Detail_model extends CI_Model
{

	static $TABLE;
	public $error;

	function __construct()
	{
		parent::__construct();
		//选择数据库
		$this->load->library('test_database');
		$this->test_database->test();
		$this->load->database($this->test_database->get_db());
		//载入模型
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		//载入类库
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();
		//初始化数据
		$this->TABLE = 'sale_advices_details';		//数据表
		$this->TEMP_TABLE = 'temp_' . $this->TABLE;	//临时数据表
		$this->error = FALSE;		//错误
		$this->mode = FALSE;		//处理模式
	}

	/***
	For private function here
	***/

	/***
	For public function here
	***/

	//创建销售结算报表
	public function create_detail($sale_advice_id, $stock_details, $view)
	{

		$this->load->model('stock_detail_model');
		$this->load->model('sale_advice_model');
		$this->load->model('sale_advice_stock_model');
		$this->load->model('stock_advices_details_model');
		$this->load->model('sub_goods_model');

		//初始化容器
		$sub_goods_quantity = array();	//记录销售数量
		$sub_goods_return_quantity = array();	//记录退货数量
		$sub_goods_price = array();		//记录销售成本单价
		$sub_goods_info = array();
		$sale_quantity = 0;		//记录销售总数
		$sale_value = 0;		//记录销售货物总成本
		$return_quantity = 0;	//记录退货总数
		$return_value = 0;		//记录退货总值

		//获得所有库类型
		$this->Inbound_Type_model->get_all('ID_FLG');
		$this->Outbound_Type_model->get_all('ID_FLG');

		//遍历指定的库存单，收集信息
		foreach ($stock_details as $key=>$detail)
		{

			//判断是否是销售类型
			if ('outbound' == $detail->stock_type && $this->Outbound_Type_model->is_sale($detail->bound_type_id))
			{
				$quantity = $detail->quantity;
			}
			//判断是否是退货入库类型
			else if ('inbound' == $detail->stock_type && $this->Inbound_Type_model->is_return($detail->bound_type_id))
			{
				$quantity = -$detail->quantity;
			}
			else
			{
				continue;
			}

			//赋值以方便算法
			$sub_goods_id = $detail->sub_goods_id;
			$store_id = $detail->store_id;
			$price = $detail->price;

			//数量大于0则是销售
			if ($quantity > 0)
			{
				if (isset($sub_goods_quantity[$sub_goods_id][$store_id]))
				{
					$sub_goods_quantity[$sub_goods_id][$store_id] += $quantity;
				}
				else
				{
					$sub_goods_quantity[$sub_goods_id][$store_id] = $quantity;
				}
				if (!isset($sub_goods_return_quantity[$sub_goods_id][$store_id]))
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] = 0;
				}
			}
			//数量小于0则是退货
			else
			{
				if (isset($sub_goods_return_quantity[$sub_goods_id][$store_id]))
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] += abs($quantity);
				}
				else
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] = abs($quantity);
				}
				if (!isset($sub_goods_quantity[$sub_goods_id][$store_id]))
				{
					$sub_goods_quantity[$sub_goods_id][$store_id] = 0;
				}
			}

			//累积子货物销售数量记录
			/***
			if (isset($sub_goods_quantity[$sub_goods_id][$store_id]))
			{
				$sub_goods_quantity[$sub_goods_id][$store_id] += abs($quantity);
				if ($quantity < 0)
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] += abs($quantity);
				}
			}
			else
			{
				$sub_goods_quantity[$sub_goods_id][$store_id] = abs($quantity);
				if ($quantity < 0)
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] = abs($quantity);
				}
				else
				{
					$sub_goods_return_quantity[$sub_goods_id][$store_id] = 0;
				}
			}
			***/

			//子货物销售成本单价
			if (!isset($sub_goods_price[$sub_goods_id]))
			{
				$sub_goods_price[$sub_goods_id]['sub_goods_id'] = $sub_goods_id;
				$sub_goods_price[$sub_goods_id] = $price;
			}

			//累积销售货物成本、数量
			$sale_quantity += abs($quantity);
			$sale_value += abs($quantity) * $price;

			//累积销售退货货物值
			if ($quantity <= 0)
			{
				$return_quantity += abs($quantity);
				$return_value += abs($quantity * $price);
			}

		}

		//更新销售结算报表其他数据
		$data = array(
			'sale_quantity'		=> $sale_quantity,
			'sale_value'		=> $sale_value,
			'return_quantity'	=> $return_quantity,
			'return_value'		=> $return_value
			);
		if (FALSE === $this->sale_advice_model->update_rows($data, $sale_advice_id))
		{
			$this->error = $this->sale_advice_model->error;
			return FALSE;
		}

		//获得当前所有的子货物

		//创建销售结算报表销售成本详情

		//获得当前的所有子货物
		$sub_goods = $this->sub_goods_model->get_all();

		//格式化所有子货物并关联主货物
		foreach ($sub_goods as $goods)
		{
			//获得子货物所有信息，包括主货物信息
			if ($goods_info = $this->sub_goods_model->get_goods_info($goods->sub_goods_id))
			{
				//添加需要插入的数据
				$sub_goods_info[$goods->sub_goods_id]['attribute'] = $goods_info['sub_goods']['attribute'];
				$sub_goods_info[$goods->sub_goods_id]['number'] = $goods_info['sub_goods']['number'];
				$sub_goods_info[$goods->sub_goods_id]['search_code'] = $goods_info['sub_goods']['search_code'];
				$sub_goods_info[$goods->sub_goods_id]['main_goods_id'] = $goods_info['main_goods']['main_goods_id'];
				$sub_goods_info[$goods->sub_goods_id]['name'] = $goods_info['main_goods']['name'];

				//判断插入图片
				if ($goods_info['sub_goods']['image'])
				{
					$sub_goods_info[$goods->sub_goods_id]['image'] = $goods_info['sub_goods']['image'];
				}
				else if ($goods_info['main_goods']['taobao_info']['image'])
				{
					$sub_goods_info[$goods->sub_goods_id]['image'] = $goods_info['main_goods']['taobao_info']['image'];
				}
				else
				{
					$sub_goods_info[$goods->sub_goods_id]['image'] = '';
				}
				unset($goods_info);
			}
			else
			{
				$this->error = $this->sub_goods_model->error;
				return FALSE;
			}

		}

		foreach ($sub_goods_quantity as $sub_goods_id=>$quantities)
		{
			if (FALSE === $this->create_single_detail($sale_advice_id, $sub_goods_id, array_sum($quantities), array_sum($sub_goods_return_quantity[$sub_goods_id]), $sub_goods_price[$sub_goods_id], $sub_goods_info[$sub_goods_id]['attribute'], $sub_goods_info[$sub_goods_id]['number'], $sub_goods_info[$sub_goods_id]['search_code'], $sub_goods_info[$sub_goods_id]['main_goods_id'], $sub_goods_info[$sub_goods_id]['name'], $sub_goods_info[$sub_goods_id]['image']))
			{
				return FALSE;
			}
			else
			{
				unset($sub_goods_info[$sub_goods_id]);
			}
		}

		//遍历剩下的子货物创建结算数据
		foreach ($sub_goods_info as $sub_goods_id=>$info)
		{
			$price = $this->stock_advices_details_model->get_single_price($sale_advice_id, $sub_goods_id);
			if (FALSE === $this->create_single_detail($sale_advice_id, $sub_goods_id, 0, 0, $price, $sub_goods_info[$sub_goods_id]['attribute'], $sub_goods_info[$sub_goods_id]['number'], $sub_goods_info[$sub_goods_id]['search_code'], $sub_goods_info[$sub_goods_id]['main_goods_id'], $sub_goods_info[$sub_goods_id]['name'], $sub_goods_info[$sub_goods_id]['image']))
			{
				return FALSE;
			}
		}

		//创建销售结算的销售数量详情
		if (FALSE === $this->sale_advice_stock_model->create_stocks($sale_advice_id, $sub_goods_quantity, $sub_goods_return_quantity))
		{
			$this->error = $this->sale_advice_stock_model->error;
			return FALSE;
		}

		return TRUE;

	}

	public function create_single_detail($sale_advice_id, $sub_goods_id, $sub_goods_quantity, $sub_goods_return_quantity, $sub_goods_price, $attribute, $number, $search_code, $main_goods_id, $name, $image)
	{
		try {
			$data = array(
				'sale_advice_id'			=> $sale_advice_id,
				'sub_goods_id'				=> $sub_goods_id,
				'sub_goods_quantity'		=> $sub_goods_quantity,
				'sub_goods_return_quantity'	=> $sub_goods_return_quantity,
				'sub_goods_price'			=> $sub_goods_price,
				'attribute'					=> $attribute,
				'number'					=> $number,
				'search_code'				=> $search_code,
				'main_goods_id'				=> $main_goods_id,
				'name'						=> $name,
				'image'						=> $image,
				'create_date'				=> time()
				);
			if ($this->db->insert($this->TABLE, $data))
			{
				return TRUE;
			}
			else
			{
				$this->error = '不能创建单条销售结算记录 SUB_GOODS_ID' . $sub_goods_id;
				return FALSE;
			}
		} catch (Exception $e) {
			$this->error = $e;
			return FALSE;
		}
	}

	public function get_advice_details($sale_advice_id)
	{

		$this->db->where('sale_advice_id = '.$sale_advice_id);
		$query = $this->db->get($this->TABLE);
		if ($query->num_rows() > 0)
		{
			$advice_details = $query->result();

			//取得结算货物的库存信息并重构数据
			$advice_stocks = $this->sale_advice_stock_model->get_stock($sale_advice_id, TRUE);

			foreach ($advice_details as $key=>$stock)
			{
				$advice_details[$key]->quantity = $advice_stocks[$stock->sub_goods_id];
			}
			return $advice_details;
		}
		else
		{
			return array();
		}
	}

	//获得单条最新的记录
	public function get_single_detail($type, $id, $return='')
	{
		$this->db->where($type, $id);
		$this->db->order_by('stock_adv_detail_id DESC');
		$this->db->limit(1);
		$query = $this->db->get($this->TABLE);
		if ($query->num_rows() > 0)
		{
			$result = $query->row();
			if ('PRICE' === $return)
			{
				return $result->sub_goods_price;
			}
			else
			{
				return $result;
			}
		}
		else
		{
			return array();
		}
	}

	//获得单条成本记录
	public function get_single_price($sale_advice_id, $sub_goods_id)
	{
		$this->db->where('sale_advice_id', $sale_advice_id);
		$this->db->where('sub_goods_id', $sub_goods_id);
		$this->db->limit(1);
		$result = $this->db->get($this->TABLE)->row();
		if (!empty($result))
		{
			return $result->sub_goods_price;
		}
		else
		{
			return 0;
		}
	}

}